import config from '@/config'
import { appsAPI } from '@/services/common/api'
import { getQywxs } from '@/services/qywxs'

const TOOL_ID = 'quntooldocker'

export function useConfirmQywxLimit() {
  const [passed, setPassed] = useState<null | string | boolean>(null) // null: 确认中，string：错误信息，false: 未通过检查，true：通过检查

  useEffect(() => {
    if (!config.LIMIT_QYWX) {
      setPassed(true)
      return
    }

    void appsAPI<{ login_count: number } | null>('/auth/auth?service=' + TOOL_ID).then(res => {
      if (!res.success) return void setPassed(res.error.message)
      const limit = res.data?.login_count ?? 0

      void getQywxs().then(res => {
        if (!res.success) return void setPassed(res.error)
        setPassed(res.data.length < limit)
      })
    })
  }, [])

  return passed
}
